package com.example.nacos_test.listener;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import sun.applet.AppletEvent;
import sun.applet.AppletListener;

@Component
@Slf4j
public class AppListener implements ApplicationListener<ContextRefreshedEvent> {

    @NacosInjected
    private NamingService namingService;

    @Value("${spring.application.name}")
    private String serviceName;
    @Value("${server.address}")
    private String ip;
    @Value("${server.port}")
    private int port;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        try {
            namingService.registerInstance(serviceName, ip, port );
        } catch (NacosException e) {
            e.printStackTrace();
        }
    }
}
